﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmOutShopBillPay : Form
    {
        public FrmOutShopBillPay()
        {
            InitializeComponent();
            gridViewBill.ShowRowIndicator(50);
        }
        private static FrmOutShopBillPay instance = null;
        public static FrmOutShopBillPay GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmOutShopBillPay();

                }
                return instance;
            }
        }
        OutShopBillBLL _bll = new OutShopBillBLL();
        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            OutProcessShop_Info info = new OutProcessShop_Info();
            if (!string.IsNullOrEmpty(textEditbianhao.Text.Trim()))
            {
               CurrList= _bll.GetBill(Convert.ToInt32(textEditbianhao.Text.Trim()), out info);
              
                comboBoxOutShop.Text = info.OutShopName;
            }
            else
            {
                CurrList = _bll.GetBill(currShopID, dateEditStartTime.Value.Date, dateEditEndTime.Value.AddDays(1).Date);
               

            }
             gridControlBill.DataSource = CurrList;
           // int receive = (int) OutProcessProductStatus.Received;
             txtAllBillMoney.Text = CurrList.Sum(p => p.SummedRecive).ToString("0.00");
        }

        private int currShopID = 0;
        private decimal _payCount;
        public decimal  PayCount
        {
           set
            {
                _payCount = value;
                this.textEditPay.Text = value.ToString("0.00");
            }
            get
            {
                return _payCount; 
            }

        }

        List<OutShop_Bill_Info> CurrList=new List<OutShop_Bill_Info>();
        private void comboBoxOutShop_SelectionChangeCommitted(object sender, EventArgs e)
        {
            currShopID = ((OutProcessShop_Info)comboBoxOutShop.SelectedItem).OutShopId;

        }
       OutShop_Bill_Info CurrInfo=new OutShop_Bill_Info();
        private void gridViewBill_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if(e.RowHandle>=0)
            {
                OutShop_Bill_Info info = (OutShop_Bill_Info) gridViewBill.GetRow(e.RowHandle);
                CurrInfo = info;
                if(e.Column.Name=="IsSelect")
                {
                    if(info.IsSelect)
                    {
                        
                        info.IsSelect = false;
                        PayCount = PayCount - (info.SummedRecive-info.SummenPay);
                    }
                    else
                    {
                        info.IsSelect = true;
                        PayCount = PayCount + (info.SummedRecive - info.SummenPay);
                    }
                }
                gridViewBill.RefreshData();
            }
        }

        private void FrmOutShopBillPay_Load(object sender, EventArgs e)
        {
            comboBoxOutShop.DataSource = _bll.GetShopList();
            comboBoxOutShop.DisplayMember = "OutShopName";
            comboBoxOutShop.ValueMember = "OutShopId";
            
        }

        private void simpleButtonEdit_Click(object sender, EventArgs e)
        {
            FrmOutShopBillEdit frm = new FrmOutShopBillEdit(CurrInfo.BillID);
            frm.ShowDialog();
        }

        private void gridViewBill_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OutShop_Bill_Info info = (OutShop_Bill_Info)gridViewBill.GetRow(e.RowHandle);
                if (e.Column.Name == "PayStatus")
                {
                    switch (info.PayStatus)
                    {
                        case (int)OutProcessPayStatus.UnPay:
                            e.DisplayText = "未付款";
                            break;
                        case (int)OutProcessPayStatus.Part:
                            e.DisplayText = "部分付款";
                            break;
                        case (int)OutProcessPayStatus.Complete:
                            e.DisplayText = "付款完毕";
                            break;
                        default:
                            e.DisplayText = "";
                            break;

                    }
                }
                if (e.Column.Name == "ProductStatus")
                {
                    switch (info.ProductSratus)
                    {
                        case (int)OutProcessProductStatus.Outting:
                            e.DisplayText = "委外中";
                            break;
                        case (int)OutProcessProductStatus.Received:
                            e.DisplayText = "部分收货";
                            break;
                        case (int)OutProcessProductStatus.Complete:
                            e.DisplayText = "完全收货";
                            break;
                        default:
                            e.DisplayText = "";
                            break;
                    }
                }
            }
        }

        private void simpleButtonSelectall_Click(object sender, EventArgs e)
        {
            
            foreach (OutShop_Bill_Info list in CurrList)
            {
                list.IsSelect = true;
            }
            decimal cout =CurrList.Sum(p => p.SummedRecive)-CurrList.Sum(p => p.SummenPay);
            PayCount = cout;
            textEditPay.Text = cout.ToString("0.00");
            gridControlBill.RefreshDataSource();
        }

        private void simpleButtonSelectNone_Click(object sender, EventArgs e)
        {
            //PayCount = 0;
            foreach (OutShop_Bill_Info list in CurrList)
            {
                if(list.IsSelect)
                {
                    list.IsSelect = false;
                }
                else
                {
                    list.IsSelect = true;
                }
            }
            decimal count =CurrList.Where(p => p.IsSelect).Sum(p => p.SummedRecive)- CurrList.Where(p => p.IsSelect).Sum(p => p.SummenPay);
            PayCount = count;
            textEditPay.Text = count.ToString("0.00");
            gridControlBill.RefreshDataSource();
        }

        private void btnActiveOrder_Click(object sender, EventArgs e)
        {
            if(System.Windows.Forms.DialogResult.OK==MessageBox.Show("确定要冲抵掉所选的委外单据","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question))
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    decimal clearMoney = Convert.ToDecimal(txtClearMoney.Text.Trim()==""?"0.00":txtClearMoney.Text.Trim());

                    _bll.CrushOutshop(CurrList.Where(p => p.IsSelect).ToList(), PayCount, clearMoney, txtClearRemark.Text.Trim(), 0, currShopID);
                    this.PayCount = 0;
                    MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    simpleButtonOK_Click(null, null);
                }
                catch (Exception exception)
                {

                    MessageBox.Show("数据连接错误，请稍后再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }finally
                {
                    this.Cursor = Cursors.Default;
                }
                

            }
        }

        private void FrmOutShopBillPay_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        

    }
}
